package net.tv90.bfmccore.util.area;

import lombok.Data;
import net.tv90.bfmccore.util.vector.Vector2i;
import net.tv90.bfmccore.util.vector.Vector3i;

import java.util.ArrayList;
import java.util.List;

/**
 * 由一系列二维整形向量在笛卡尔坐标系下张成的二维整形区域
 */

@Data
public class Area2i implements Area{

    //区域
    protected List<Vector2i> vectorList = new ArrayList<>();

    public Area2i(){}

    public Area2i(List<Vector2i> vectorList) {
        this.vectorList = vectorList;
    }

    /**
     * 判断点是否位于区域内
     * @param point3i 点
     * @return true or false
     */
    public boolean isInArea(Vector3i point3i){
        Vector2i point = point3i.reduction();
        if(point == null || vectorList == null) return false;
        int N = vectorList.size();
        boolean boundOrVertex = true; //如果点位于多边形的顶点或边上，也算做点在多边形内，直接返回true
        int intersectCount = 0;//cross points count of x
        double precision = 2e-10; //浮点类型计算时候与0比较时候的容差
        Vector2i p1, p2;//neighbour bound vertices

        p1 = vectorList.get(0);//left vertex
        for(int i = 1; i <= N; ++i){//check all rays
            if(point.equals(p1)){
                return boundOrVertex;//p is an vertex
            }

            p2 = vectorList.get(i % N);//right vertex
            if(point.x < Math.min(p1.x, p2.x) || point.x > Math.max(p1.x, p2.x)){//ray is outside of our interests
                p1 = p2;
                continue;//next ray left point
            }

            if(point.x > Math.min(p1.x, p2.x) && point.x < Math.max(p1.x, p2.x)){//ray is crossing over by the algorithm (common part of)
                if(point.y <= Math.max(p1.y, p2.y)){//x is before of ray
                    if(p1.x == p2.x && point.y >= Math.min(p1.y, p2.y)){//overlies on a horizontal ray
                        return boundOrVertex;
                    }

                    if(p1.y == p2.y){//ray is vertical
                        if(p1.y == point.y){//overlies on a vertical ray
                            return boundOrVertex;
                        }else{//before ray
                            ++intersectCount;
                        }
                    }else{//cross point on the left side
                        double xinters = (point.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;//cross point of y
                        if(Math.abs(point.y - xinters) < precision){//overlies on a ray
                            return boundOrVertex;
                        }
                        if(point.y < xinters){//before ray
                            ++intersectCount;
                        }
                    }
                }
            }else{//special case when ray is crossing through the vertex
                if(point.x == p2.x && point.y <= p2.y){//p crossing over p2
                    Vector2i p3 = vectorList.get((i+1) % N); //next vertex
                    if(point.x >= Math.min(p1.x, p3.x) && point.x <= Math.max(p1.x, p3.x)){//p.x lies between p1.x & p3.x
                        ++intersectCount;
                    }else{
                        intersectCount += 2;
                    }
                }
            }
            p1 = p2;//next ray left point
        }

        //偶数在多边形外，奇数在多边形内
        return intersectCount % 2 != 0;
    }


    public List<Vector2i> getVectorList() {
        return vectorList;
    }

    public void setVectorList(List<Vector2i> vectorList) {
        this.vectorList = vectorList;
    }

    @Override
    public String toString() {
        return "Area{" + vectorList + "}";
    }
}
